﻿<!--
// (c) Copyright Microsoft Corporation.
// This source is subject to the Microsoft Public License (Ms-PL).
// Please see http://go.microsoft.com/fwlink/?LinkID=131993 for details.
// All other rights reserved.
-->

<UserControl x:Class="Microsoft.Windows.Controls.Samples.AutoCompleteLambda"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:basics="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls"
    xmlns:input="clr-namespace:Microsoft.Windows.Controls;assembly=Microsoft.Windows.Controls.Input"
    xmlns:controls="clr-namespace:Microsoft.Windows.Controls;assembly=Microsoft.Windows.Controls">
    <StackPanel>
        <StackPanel.Resources>

            <!-- DataTemplate for airports -->
            <DataTemplate x:Key="AirportDataTemplate">
                <Grid Width="370">
                    <Grid.Background>
                        <SolidColorBrush Color="#aa000000" />
                    </Grid.Background>
                    <TextBlock Foreground="#22ffffff" Margin="4+0,2+0" FontSize="34" Text="{Binding CodeFaa}" />
                    <StackPanel HorizontalAlignment="Right" Margin="0, 0, 8, 0">
                        <TextBlock HorizontalAlignment="Right" Foreground="White" 
                               Text="{Binding LimitedName}" Padding="2" />
                        <TextBlock HorizontalAlignment="Right" Foreground="White" 
                               FontSize="14" Text="{Binding City}" Padding="2" />
                        <TextBlock HorizontalAlignment="Right" Foreground="White" 
                               FontSize="14" Text="{Binding State}" Padding="2" />
                    </StackPanel>
                </Grid>
            </DataTemplate>
        </StackPanel.Resources>

        <!-- Custom lambda and template -->
        <ContentControl Content="ItemFilter with a lambda &amp; a custom DataTemplate" Style="{StaticResource Header}" />
        
        <StackPanel>
            <TextBlock Style="{StaticResource Information}">
                Search for and select your airports. Once you have made your 
                selections, click to search for tickets using Live Farecast.
            </TextBlock>
            
            <Grid Margin="0, 16, 0, 16">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="180" />
                    <ColumnDefinition Width="*" />
                </Grid.ColumnDefinitions>
                
                <TextBlock Padding="5">Departure:</TextBlock>
                
                <StackPanel Grid.Column="1">
                    <controls:AutoCompleteBox 
                        x:Name="DepartureAirport" 
                        MinimumPrefixLength="1" 
                        SearchMode="Custom" 
                        Width="400" 
                        IsTextCompletionEnabled="False"
                        HorizontalAlignment="Left" 
                        ItemTemplate="{StaticResource AirportDataTemplate}" 
                        />
                    <basics:DatePicker 
                        x:Name="DepartureDate" 
                        HorizontalAlignment="Left"
                        Width="125" />
                </StackPanel>
            </Grid>

            <Grid Margin="0, 16, 0, 16">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="180" />
                    <ColumnDefinition Width="*" />
                </Grid.ColumnDefinitions>

                <TextBlock Padding="5">Arrival:</TextBlock>

                <StackPanel Grid.Column="1">
                    <controls:AutoCompleteBox 
                        x:Name="ArrivalAirport" 
                        MinimumPrefixLength="1" 
                        SearchMode="Custom" 
                        IsTextCompletionEnabled="False"
                        Width="400" 
                        HorizontalAlignment="Left" 
                        ItemTemplate="{StaticResource AirportDataTemplate}" 
                        />
                    <basics:DatePicker 
                        x:Name="ArrivalDate" 
                        HorizontalAlignment="Left"
                        Width="125" />
                </StackPanel>
            </Grid>

            <Grid Margin="0, 16, 0, 16">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="180" />
                    <ColumnDefinition Width="*" />
                </Grid.ColumnDefinitions>

                <TextBlock Padding="5">Passengers:</TextBlock>

                <input:NumericUpDown 
                    Minimum="1"
                    Maximum="10"
                    x:Name="Passengers"
                    HorizontalAlignment="Left"
                    Grid.Column="1" />
            </Grid>

            <Grid Margin="0, 16, 0, 16">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="180" />
                    <ColumnDefinition Width="*" />
                </Grid.ColumnDefinitions>

                <TextBlock Padding="5">Book flight:</TextBlock>

                <Button x:Name="BookFlight" 
                        IsEnabled="False"
                        Grid.Column="1"
                        Padding="6"
                        HorizontalContentAlignment="Left"
                        HorizontalAlignment="Left"
                        Content="Search Live Farecast..."
                        />
            </Grid>

            <ContentControl Content="Customizing the filter algorithm" Style="{StaticResource Header}" />
            <TextBlock Style="{StaticResource Information}">
            The above sample uses a custom ItemFilter lambda to match several 
            airport properties against the search string, allowing a user to type an 
            airport code, city name, or airport name into the field.

            <LineBreak /><LineBreak />

            Each filter method takes two parameters: the search string (the 
            value of the text box), a second item parameter, and then returns 
            either true or false. A true return value means that the item 
            should appear in the list of suggestions offered by the 
            AutoCompleteBox control.

            <LineBreak /><LineBreak />

            As a developer, you can take the search string and tokenize it, 
            parse it for an expression or words, or find a new creative way 
            of searching through your data and surfacing it through the control.
            </TextBlock>


        </StackPanel>

        <src:SourceViewer xmlns:src="clr-namespace:Microsoft.Windows.Controls.Samples;assembly=Microsoft.Windows.Controls.Samples.Common" xmlns:sys="clr-namespace:System;assembly=mscorlib">
  <src:SourceFile Path="AutoCompleteLambda.xaml">
    <src:SourceFile.Source>
      <sys:String>&lt;!--
// (c) Copyright Microsoft Corporation.
// This source is subject to the Microsoft Public License (Ms-PL).
// Please see http://go.microsoft.com/fwlink/?LinkID=131993 for details.
// All other rights reserved.
--&gt;

&lt;UserControl x:Class="Microsoft.Windows.Controls.Samples.AutoCompleteLambda"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:basics="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls"
    xmlns:input="clr-namespace:Microsoft.Windows.Controls;assembly=Microsoft.Windows.Controls.Input"
    xmlns:controls="clr-namespace:Microsoft.Windows.Controls;assembly=Microsoft.Windows.Controls"&gt;
    &lt;StackPanel&gt;
        &lt;StackPanel.Resources&gt;

            &lt;!-- DataTemplate for airports --&gt;
            &lt;DataTemplate x:Key="AirportDataTemplate"&gt;
                &lt;Grid Width="370"&gt;
                    &lt;Grid.Background&gt;
                        &lt;SolidColorBrush Color="#aa000000" /&gt;
                    &lt;/Grid.Background&gt;
                    &lt;TextBlock Foreground="#22ffffff" Margin="4+0,2+0" FontSize="34" Text="{Binding CodeFaa}" /&gt;
                    &lt;StackPanel HorizontalAlignment="Right" Margin="0, 0, 8, 0"&gt;
                        &lt;TextBlock HorizontalAlignment="Right" Foreground="White" 
                               Text="{Binding LimitedName}" Padding="2" /&gt;
                        &lt;TextBlock HorizontalAlignment="Right" Foreground="White" 
                               FontSize="14" Text="{Binding City}" Padding="2" /&gt;
                        &lt;TextBlock HorizontalAlignment="Right" Foreground="White" 
                               FontSize="14" Text="{Binding State}" Padding="2" /&gt;
                    &lt;/StackPanel&gt;
                &lt;/Grid&gt;
            &lt;/DataTemplate&gt;
        &lt;/StackPanel.Resources&gt;

        &lt;!-- Custom lambda and template --&gt;
        &lt;ContentControl Content="ItemFilter with a lambda &amp;amp; a custom DataTemplate" Style="{StaticResource Header}" /&gt;
        
        &lt;StackPanel&gt;
            &lt;TextBlock Style="{StaticResource Information}"&gt;
                Search for and select your airports. Once you have made your 
                selections, click to search for tickets using Live Farecast.
            &lt;/TextBlock&gt;
            
            &lt;Grid Margin="0, 16, 0, 16"&gt;
                &lt;Grid.ColumnDefinitions&gt;
                    &lt;ColumnDefinition Width="180" /&gt;
                    &lt;ColumnDefinition Width="*" /&gt;
                &lt;/Grid.ColumnDefinitions&gt;
                
                &lt;TextBlock Padding="5"&gt;Departure:&lt;/TextBlock&gt;
                
                &lt;StackPanel Grid.Column="1"&gt;
                    &lt;controls:AutoCompleteBox 
                        x:Name="DepartureAirport" 
                        MinimumPrefixLength="1" 
                        SearchMode="Custom" 
                        Width="400" 
                        IsTextCompletionEnabled="False"
                        HorizontalAlignment="Left" 
                        ItemTemplate="{StaticResource AirportDataTemplate}" 
                        /&gt;
                    &lt;basics:DatePicker 
                        x:Name="DepartureDate" 
                        HorizontalAlignment="Left"
                        Width="125" /&gt;
                &lt;/StackPanel&gt;
            &lt;/Grid&gt;

            &lt;Grid Margin="0, 16, 0, 16"&gt;
                &lt;Grid.ColumnDefinitions&gt;
                    &lt;ColumnDefinition Width="180" /&gt;
                    &lt;ColumnDefinition Width="*" /&gt;
                &lt;/Grid.ColumnDefinitions&gt;

                &lt;TextBlock Padding="5"&gt;Arrival:&lt;/TextBlock&gt;

                &lt;StackPanel Grid.Column="1"&gt;
                    &lt;controls:AutoCompleteBox 
                        x:Name="ArrivalAirport" 
                        MinimumPrefixLength="1" 
                        SearchMode="Custom" 
                        IsTextCompletionEnabled="False"
                        Width="400" 
                        HorizontalAlignment="Left" 
                        ItemTemplate="{StaticResource AirportDataTemplate}" 
                        /&gt;
                    &lt;basics:DatePicker 
                        x:Name="ArrivalDate" 
                        HorizontalAlignment="Left"
                        Width="125" /&gt;
                &lt;/StackPanel&gt;
            &lt;/Grid&gt;

            &lt;Grid Margin="0, 16, 0, 16"&gt;
                &lt;Grid.ColumnDefinitions&gt;
                    &lt;ColumnDefinition Width="180" /&gt;
                    &lt;ColumnDefinition Width="*" /&gt;
                &lt;/Grid.ColumnDefinitions&gt;

                &lt;TextBlock Padding="5"&gt;Passengers:&lt;/TextBlock&gt;

                &lt;input:NumericUpDown 
                    Minimum="1"
                    Maximum="10"
                    x:Name="Passengers"
                    HorizontalAlignment="Left"
                    Grid.Column="1" /&gt;
            &lt;/Grid&gt;

            &lt;Grid Margin="0, 16, 0, 16"&gt;
                &lt;Grid.ColumnDefinitions&gt;
                    &lt;ColumnDefinition Width="180" /&gt;
                    &lt;ColumnDefinition Width="*" /&gt;
                &lt;/Grid.ColumnDefinitions&gt;

                &lt;TextBlock Padding="5"&gt;Book flight:&lt;/TextBlock&gt;

                &lt;Button x:Name="BookFlight" 
                        IsEnabled="False"
                        Grid.Column="1"
                        Padding="6"
                        HorizontalContentAlignment="Left"
                        HorizontalAlignment="Left"
                        Content="Search Live Farecast..."
                        /&gt;
            &lt;/Grid&gt;

            &lt;ContentControl Content="Customizing the filter algorithm" Style="{StaticResource Header}" /&gt;
            &lt;TextBlock Style="{StaticResource Information}"&gt;
            The above sample uses a custom ItemFilter lambda to match several 
            airport properties against the search string, allowing a user to type an 
            airport code, city name, or airport name into the field.

            &lt;LineBreak /&gt;&lt;LineBreak /&gt;

            Each filter method takes two parameters: the search string (the 
            value of the text box), a second item parameter, and then returns 
            either true or false. A true return value means that the item 
            should appear in the list of suggestions offered by the 
            AutoCompleteBox control.

            &lt;LineBreak /&gt;&lt;LineBreak /&gt;

            As a developer, you can take the search string and tokenize it, 
            parse it for an expression or words, or find a new creative way 
            of searching through your data and surfacing it through the control.
            &lt;/TextBlock&gt;


        &lt;/StackPanel&gt;

    &lt;/StackPanel&gt;
&lt;/UserControl&gt;
</sys:String>
    </src:SourceFile.Source>
  </src:SourceFile>
  <src:SourceFile Path="AutoCompleteLambda.xaml.cs">
    <src:SourceFile.Source>
      <sys:String>// (c) Copyright Microsoft Corporation.
// This source is subject to the Microsoft Public License (Ms-PL).
// Please see http://go.microsoft.com/fwlink/?LinkID=131993 for details.
// All other rights reserved.

using System;
using System.Globalization;
using System.Windows;
using System.Windows.Browser;
using System.Windows.Controls;
using System.ComponentModel;

namespace Microsoft.Windows.Controls.Samples
{
    /// &lt;summary&gt;
    /// The AutoCompleteLambda sample page demonstrates using a custom data 
    /// template, binding to elements in a rich CLR type, and using a lambda 
    /// expression to provide custom search filtering capabilities to the 
    /// AutoCompleteBox control.
    /// &lt;/summary&gt;
    [Sample("(1)ItemFilter lambda", DifficultyLevel.Basic)]
    [Category("AutoCompleteBox")]
    public partial class AutoCompleteLambda : UserControl
    {
        /// &lt;summary&gt;
        /// Initializes a new instance of the AutoCompleteLambda class.
        /// &lt;/summary&gt;
        public AutoCompleteLambda()
        {
            InitializeComponent();
            Loaded += new RoutedEventHandler(Airports_Loaded);
        }

        /// &lt;summary&gt;
        /// Handle the Loaded event of the page.
        /// &lt;/summary&gt;
        /// &lt;param name="sender"&gt;The source object.&lt;/param&gt;
        /// &lt;param name="e"&gt;The event arguments.&lt;/param&gt;
        private void Airports_Loaded(object sender, RoutedEventArgs e)
        {
            // Provide airport data and a custom filter
            ObjectCollection airports = Airport.SampleAirports;
            DepartureAirport.ItemsSource = airports;
            ArrivalAirport.ItemsSource = airports;
            DepartureAirport.ItemFilter = (search, item) =&gt;
            {
                Airport airport = item as Airport;
                if (airport != null)
                {
                    // Interested in: Name, City, FAA code
                    string filter = search.ToUpper(CultureInfo.InvariantCulture);
                    return (airport.CodeFaa.ToUpper(CultureInfo.InvariantCulture).Contains(filter)
                        || airport.City.ToUpper(CultureInfo.InvariantCulture).Contains(filter)
                        || airport.Name.ToUpper(CultureInfo.InvariantCulture).Contains(filter));
                }

                return false;
            };
            ArrivalAirport.ItemFilter = DepartureAirport.ItemFilter;

            // Look for changes
            DepartureAirport.SelectionChanged += SelectedItemsChanged;
            ArrivalAirport.SelectionChanged += SelectedItemsChanged;
            DepartureDate.SelectedDateChanged += SelectedItemsChanged;
            ArrivalDate.SelectedDateChanged += SelectedItemsChanged;

            // Set the date defaults in code
            DepartureDate.SelectedDate = DateTime.UtcNow + TimeSpan.FromDays(7);
            ArrivalDate.SelectedDate = DateTime.UtcNow + TimeSpan.FromDays(14);

            // Navigate to Farecast
            BookFlight.Click += BookFlight_Click;
        }

        /// &lt;summary&gt;
        /// The button to book the flight has been clicked.
        /// &lt;/summary&gt;
        /// &lt;param name="sender"&gt;The source object.&lt;/param&gt;
        /// &lt;param name="e"&gt;The event data.&lt;/param&gt;
        private void BookFlight_Click(object sender, RoutedEventArgs e)
        {
            Airport d = (Airport)DepartureAirport.SelectedItem;
            Airport a = (Airport)ArrivalAirport.SelectedItem;
            DateTime dd = (DateTime)DepartureDate.SelectedDate;
            DateTime ad = (DateTime)ArrivalDate.SelectedDate;
            int p = (int)Passengers.Value;

            Uri farecast = WebServiceHelper.CreateAirfareSearchUri(d, a, dd, ad, p);
            HtmlPage.Window.Navigate(farecast, "_new");
        }

        /// &lt;summary&gt;
        /// Update the form when valid values are present in the controls.
        /// &lt;/summary&gt;
        /// &lt;param name="sender"&gt;The source object.&lt;/param&gt;
        /// &lt;param name="e"&gt;The event data.&lt;/param&gt;
        private void SelectedItemsChanged(object sender, SelectionChangedEventArgs e)
        {
            BookFlight.IsEnabled = DepartureAirport.SelectedItem != null
                &amp;&amp; ArrivalAirport.SelectedItem != null
                &amp;&amp; DepartureDate.SelectedDate != null
                &amp;&amp; ArrivalDate.SelectedDate != null
                &amp;&amp; Passengers.Value &gt; 0;
        }
    }
}
</sys:String>
    </src:SourceFile.Source>
  </src:SourceFile>
</src:SourceViewer>

    </StackPanel>
</UserControl>
